<?php namespace App\Admin\Orders\AfterSale;

//基础条件
use App\Core\Repository\Criterias\FixedCriteria;
use App\Core\Repository\Criterias\PaginateCriteria;
use App\Core\Repository\Criterias\SortCriteria;
//过滤器
use App\Core\Repository\Criterias\Filters\InputCriteria;
use App\Core\Repository\Criterias\Filters\InputKeywordsCriteria;
use App\Core\Repository\Criterias\Filters\InputTimeRangeCriteria;

use Input;
use App\Core\Exceptions\InputError;
use App\Admin\Logistics\Logistics\Repository as LogisticsRepository;
use App\Admin\Logistics\Delivers\Repository as DeliversRepository;

trait Service {
    
    //基础服务
    use \App\Core\Service\Service;
    use \App\Core\Service\Services\All;
    use \App\Core\Service\Services\Indexer;
    use \App\Core\Service\Services\Creater;
    use \App\Core\Service\Services\Updater;
    use \App\Core\Service\Services\Changer;
    use \App\Core\Service\Services\Deleter;
    use \App\Core\Service\Services\Detailer;
    //操作日志
    use \App\Admin\Base\LogService;
    
    protected function bootService(){
        $this->bootBaseService();
        $this->setRepository(new Repository);
        $this->setMeta(new Meta);
        $this->repository = $this->getRepository();
    }
	
    //列表
    public function lists(){
        //过滤器
        $conditions = [
            //new InputKeywordsCriteria('name'),
            new InputTimeRangeCriteria('created_at'),
            new InputCriteria('type'),
            new InputCriteria('state'),
            new InputCriteria('status')
        ];
        $count = $this->repository->addCriterias($conditions)->count();
        $conditions[] = new PaginateCriteria();
        $conditions[] = new SortCriteria('id', 'DESC');
        $data = $this->repository->resetQuery()->resetCriteria()->addCriterias($conditions)->all();
        $data->load('orders','user');
        return $this->success([
            'count' => $count,
            'data' => $data
        ]);
    }
    
    //通过
    public function adopt(){
        return $this->change('status', '1', '0');
    }
    
    //驳回
    public function reject(){
        return $this->change('status', '2', '0');
    }

    //给换货订单添加相应的订单号
    public function createlogistics(){
        $order_id = Input::get('order_id');
        $deliver_id = Input::get('deliver_code');
        $deliver_no = Input::get('deliver_no');

        $delivers = with(new DeliversRepository)->addCriteria(new FixedCriteria('id', $deliver_id))->first();
        $deliver_code=$delivers->code;
        if($deliver_no){
            $logisticsRepository = new LogisticsRepository();
            $model = $logisticsRepository->addCriterias([
                new FixedCriteria('order_id', $order_id),
                new FixedCriteria('type',2)
            ])->first();
            if($model){
                $model->deliver_id = $deliver_id;
                $model->deliver_code = $deliver_code;
                $model->deliver_no = $deliver_no;
                $model->save();
            }else{
                $logisticsRepository->resetQuery()->create([
                    'order_id' => $order_id,
                    'deliver_id' => $deliver_id,
                    'deliver_code' => $deliver_code,
                    'deliver_no' => $deliver_no,
                    'type' => 2
                ]);
            }

            return $this->success();
        }
        return $this->inputError('非法操作');
    }

    public function wxrefund(){
        $id = Input::get('id');
        $model = $this->repository->addCriterias([
            new FixedCriteria('id', $id),
            new FixedCriteria('type', 2)
        ])->first();
        $account = $model->account;
        $orderid = $model->order_id;

        if ($account) {
            $account = floor($account * 100);
        } else {
            return $this->inputError('非法操作');
        }

        //发送接口请求
        $postUrl = 'http://' . env('APP_API_HOST') . '/wx/returnFee';
        $curlPost = array(
            'account' => $account,
            'orderId' => $orderid,
        );
        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_URL, $postUrl);//抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        $data = curl_exec($ch);//运行curl
        curl_close($ch);

        $retArr = json_decode($data);
        if (isset($retArr->statusCode) && ($retArr->statusCode) == 0) {
            //成功
            $model->state = 1;
            $model->save();
            return $this->success();
        } else {
            //失败
            return $this->inputError('非法操作');
        }
    }


}